package com.heima.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.client.ArticleClient;
import com.heima.client.MediaClient;
import com.heima.common.dto.PageResponseResult;
import com.heima.common.dto.ResponseResult;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.domain.ApAuthor;
import com.heima.domain.WmUser;
import com.heima.user.dto.AuthDto;
import com.heima.user.entity.ApUser;
import com.heima.user.entity.ApUserRealname;
import com.heima.user.mapper.ApUserMapper;
import com.heima.user.mapper.ApUserRealnameMapper;
import com.heima.user.service.IApUserRealnameService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * APP实名认证信息表 服务实现类
 * </p>
 *
 * @author gxm
 * @since 2023-12-17
 */
@Service
public class ApUserRealnameServiceImpl extends ServiceImpl<ApUserRealnameMapper, ApUserRealname> implements IApUserRealnameService {

    @Override
    public ResponseResult findList(AuthDto dto) {
        //1. 设置分页条件
        Page<ApUserRealname> page = new Page<>(dto.getPage(), dto.getSize());

        //2. 设置业务条件
        LambdaQueryWrapper<ApUserRealname> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(dto.getStatus() != null, ApUserRealname::getStatus, dto.getStatus());

        //3. 执行查询
        this.page(page, wrapper);

        //4. 返回结果
        return new PageResponseResult(dto.getPage(), dto.getSize(), page.getTotal(), page.getRecords());
    }

    @Override
    public ResponseResult authFail(AuthDto dto) {
        //根据用户id查询是否存
        LambdaQueryWrapper<ApUserRealname> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ApUserRealname::getUserId, dto.getId());
        ApUserRealname apUserRealname = this.getOne(wrapper);
        if (apUserRealname == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }

        //更新数据
        apUserRealname.setStatus(2);//审核驳回
        apUserRealname.setReason(dto.getReason());
        this.updateById(apUserRealname);
        return ResponseResult.okResult();
    }

    @Autowired
    private ApUserMapper apUserMapper;

    @Autowired
    private MediaClient mediaClient;

    @Autowired
    private ArticleClient articleClient;

    @Override
    @GlobalTransactional //分布式事务
    public ResponseResult authPass(AuthDto dto) {

        //0. 根据id查询用户信息
        LambdaQueryWrapper<ApUserRealname> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ApUserRealname::getUserId, dto.getId());
        ApUserRealname userRealname = this.getOne(queryWrapper);
        if (userRealname == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //1. 修改用户库的app_user_realname表的status为9(审核通过)--heima-leadnews-user
        userRealname.setStatus(9);
        this.updateById(userRealname);

        //2. 修改用户库的app_user表的flag=1(自媒体人)--heima-leadnews-user
        ApUser apUser = apUserMapper.selectById(dto.getId());
        apUser.setFlag(1).setIsIdentityAuthentication(true);
        apUserMapper.updateById(apUser);

        //3. 将用户信息复制到自媒体库的wm_user表--heima-leadnews-media
        WmUser wmUser = createWmUser(apUser);

        //4. 将用户信息复制到文章库的ap_author表--heima-leadnews-article
        ApAuthor apAuthor = createApAuthor(apUser, wmUser);

        //5. 给wmUser赋apAuthorId
        wmUser.setApAuthorId(apAuthor.getId());
        mediaClient.updateWmUser(wmUser);

        return ResponseResult.okResult();
    }

    private ApAuthor createApAuthor(ApUser apUser, WmUser wmUser) {
        ApAuthor apAuthor = new ApAuthor();
        apAuthor.setName(apUser.getName());
        apAuthor.setType(2);  //0 爬取数据  1 签约合作商  2 平台自媒体人
        apAuthor.setUserId(apUser.getId());
        apAuthor.setWmUserId(wmUser.getId());
        ResponseResult<ApAuthor> responseResult = articleClient.saveApAuthor(apAuthor);
        if (responseResult.getCode() != 0) {
            throw new RuntimeException("远程调用失败");
        } else {
            return responseResult.getData();
        }
    }

    private WmUser createWmUser(ApUser apUser) {
        WmUser wmUser = new WmUser();
        wmUser.setApUserId(apUser.getId());
        //wmUser.setApAuthorId(); //文章作者id  一会儿再赋值
        wmUser.setName(apUser.getName());
        wmUser.setPassword(apUser.getPassword());
        wmUser.setSalt(apUser.getSalt());
        //wmUser.setNickname(); //无法赋值
        wmUser.setImage(apUser.getImage());
        //wmUser.setLocation(apUser.get);   //无法赋值
        wmUser.setPhone(apUser.getPhone());
        wmUser.setStatus(9);  //状态  0 暂时不可用  1 永久不可用   正常可用
        //wmUser.setEmail();  //无法赋值
        wmUser.setType(0);  //账号类型 0 个人  1 企业 2 子账号
        wmUser.setScore(0);
        //wmUser.setLoginTime();   //无法赋值

        ResponseResult<WmUser> responseResult = mediaClient.saveWmUser(wmUser);
        if (responseResult.getCode() != 0) {
            throw new RuntimeException("远程调用失败");
        } else {
            return responseResult.getData();
        }
    }
}
